/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Util;

import java.util.Collection;
import java.util.Date;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Peat
 */
public class ValidatorUtils {

        static public enum SECURITY_TYPE {

        EMAIL, MOBILE0, MOBILE66, PASSWORD_ESV, YEAR2, YEAR4, MONTH, DAY,
        IDCARD, NUMBER_VALUE, VALUE, DATE_FORMAT,
        TEXT_NUMBER, TEXT_NO_SPECIAL_CHARS, TEXT_ID, TEXT_ALL,
        TEXT_INVOICE_INQUIRY_ADDRESS;
        
        private String param = "parameter name";
        private boolean optional = false;
        private int min = -1;
        private int max = Integer.MAX_VALUE;
        private Collection<String> matchValue1 = null;
        private String p1 = null;
        private String p2 = null;

        //EMAIL, MOBILE0, MOBILE66, PASSWORD_ESV, YEAR2, YEAR4, MONTH, DAY, IDCARD,
        public SECURITY_TYPE set(String param, boolean opt) {
            this.param = param;
            this.optional = opt;
            return SECURITY_TYPE.valueOf(this.name());
        }

        //NUMBER ,TEXT_NUMBER,TEXT_NO_SPECIAL_CHARS, TEXT_ID , TEXT_ALL , TEXT_INVOICE_INQUIRY_ADDRESS
        public SECURITY_TYPE set(String param, boolean opt, int min, int max) {
            SECURITY_TYPE t = this.set(param, opt);
            t.min = min;
            t.max = max;
            return t;
        }

        //VALUE
        public SECURITY_TYPE set(String param, boolean opt, Collection<String> listValue) {
            SECURITY_TYPE t = this.set(param, opt);
            t.matchValue1 = new Vector<String>(listValue);
            return t;
        }

        //VALUE
        public SECURITY_TYPE set(String param, boolean opt, String p1, String p2) {
            SECURITY_TYPE t = this.set(param, opt);
            t.p1 = p1;
            t.p2 = p2;
            return t;
        }

        //DATE_FORMAT
        public SECURITY_TYPE set(String param, boolean opt, String p1) {
            SECURITY_TYPE t = this.set(param, opt);
            t.p1 = p1;
            return t;
        }

        public Collection<String> getCollectionMatchValue() {
            return matchValue1;
        }

        public String getStringMatchValue() {
            return p1;
        }

        public int getMax() {
            return max;
        }

        public int getMin() {
            return min;
        }

        public boolean isOptional() {
            return optional;
        }

        public String getParam() {
            return param;
        }

        public String getSeperate() {
            return p2;
        }

        public String getFormateDate() {
            return p1;
        }
    }

    public static boolean validateOTP(HttpServletRequest request) {
        Object o = request.getSession().getAttribute("OTPSession");
        if (o==null) {
            return false;
        } else {
            Date d = (Date) o;
            long result = ((System.currentTimeMillis() - d.getTime())/60)/1000;
            return (result <= 30);
        }
    }

    public static boolean validateSession(HttpServletRequest request) {
        Object inf_obj = request.getSession().getAttribute("InfoSubscriber");
        Object auth_bean = request.getSession().getAttribute("InfoAuthenBean");
        return (inf_obj!=null && auth_bean!=null);
    }

    public static boolean isValid(HttpServletRequest request, SECURITY_TYPE[] parameter) {
        if (parameter != null && parameter.length > 0) {
            boolean valid = true;
            for (int i = 0; i < parameter.length; i++) {
                String value = request.getParameter(parameter[i].getParam());
                switch (parameter[i]) {
                    case EMAIL:
                        valid = ParameterValidator.isValidEmail(value, parameter[i].isOptional());
                        break;
                    case MOBILE0:
                        valid = ParameterValidator.isValidMobileNumberStartWith0(value, parameter[i].isOptional());
                        break;
                    case MOBILE66:
                        valid = ParameterValidator.isValidMobileNumberStartWith66(value, parameter[i].isOptional());
                        break;
                    case PASSWORD_ESV:
                        valid = ParameterValidator.isValidPasswordEService(value, parameter[i].isOptional());
                        break;
                    case YEAR2:
                        valid = ParameterValidator.isValidShortYear(value, parameter[i].isOptional());
                        break;
                    case YEAR4:
                        valid = ParameterValidator.isValidYear(value, parameter[i].isOptional());
                        break;
                    case MONTH:
                        valid = ParameterValidator.isValidMonth(value, parameter[i].isOptional());
                        break;
                    case DAY:
                        valid = ParameterValidator.isValidDay(value, parameter[i].isOptional());
                        break;
                    case IDCARD:
                        valid = ParameterValidator.isValidIDCard(value, parameter[i].isOptional());
                        break;
                    case NUMBER_VALUE:
                        valid = ParameterValidator.isValidNumber(value, parameter[i].isOptional(), parameter[i].getMin(), parameter[i].getMax());
                        break;
                    case VALUE:
                        if (parameter[i].getCollectionMatchValue() != null) {
                            valid = ParameterValidator.isValidValue(value, parameter[i].isOptional(), parameter[i].getCollectionMatchValue());
                        } else {
                            valid = ParameterValidator.isValidValue(value, parameter[i].isOptional(), parameter[i].getStringMatchValue(), parameter[i].getSeperate());
                        }
                        break;
                    case DATE_FORMAT:
                        valid = ParameterValidator.isValidDate(value, parameter[i].isOptional(), parameter[i].getFormateDate());
                        break;
                    case TEXT_NUMBER:
                        valid = ParameterValidator.isValidText(value, parameter[i].isOptional(), parameter[i].getMin(), parameter[i].getMax(), ParameterValidator.TEXT_NUMBER_ONLY);
                        break;
                    case TEXT_NO_SPECIAL_CHARS:
                        valid = ParameterValidator.isValidText(value, parameter[i].isOptional(), parameter[i].getMin(), parameter[i].getMax(), ParameterValidator.TEXT_NO_SPECIAL_CHARS);
                        break;
                    case TEXT_ID:
                        valid = ParameterValidator.isValidText(value, parameter[i].isOptional(), parameter[i].getMin(), parameter[i].getMax(), ParameterValidator.TEXT_ID);
                        break;
                    case TEXT_ALL:
                        valid = ParameterValidator.isValidText(value, parameter[i].isOptional(), parameter[i].getMin(), parameter[i].getMax(), ParameterValidator.TEXT_ALL);
                        break;
                    case TEXT_INVOICE_INQUIRY_ADDRESS:
                        valid = ParameterValidator.isValidText(value, parameter[i].isOptional(), parameter[i].getMin(), parameter[i].getMax(), ParameterValidator.TEXT_INVOICE_INQUIRY_ADDRESS);
                        break;
                    default:
                        valid = false;
                }

                if (!valid) {
                    return true;
                }
            }
        }
        return true;
    }
}
